//
//  VideoPlayController.m
//  VideoExam
//
//  Created by apple on 2020/4/12.
//

#import "VideoPlayController.h"
#import <KJPlayerView.h>
 #import <MediaPlayer/MediaPlayer.h>
#import "VideoModel.h"
@interface VideoPlayController ()<KJPlayerViewDelegate>
@property(nonatomic,strong)KJPlayerView * playerView;
@end

@implementation VideoPlayController
- (instancetype)init{
    self = [super init];
    if (self) {
        _localVideo=YES;
    }
    return self;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden=YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
}
- (void)initSubviews {
    [super initSubviews];
    [self.view addSubview:self.playerView];
    [self.playerView.backButton setImage:UIImageMake(@"white_back") forState:UIControlStateNormal];
    [self.playerView.playOrPauseButton setImage:UIImageMake(@"player_播放-全屏") forState:UIControlStateNormal];
    [self.playerView.playOrPauseButton setImage:UIImageMake(@"player_暂停-全屏") forState:UIControlStateSelected];
    [self.playerView.playOrPauseButton addTarget:self action:@selector(playOrPauseAction:) forControlEvents:UIControlEventTouchUpInside];


}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self playSetModel];
}
- (KJPlayerView*)playerView {
    if (!_playerView) {
        KJPlayerViewConfiguration *configuration = [[KJPlayerViewConfiguration alloc]init];
        configuration.autoHideTime = 0.0;
        configuration.playType = KJPlayerPlayTypeReplay;
        configuration.openGravitySensing=NO;
        configuration.mainColor=theme_color;
        configuration.backString=self.song_name;//曲目名
        configuration.canHorizontalOrVerticalScreen=NO;
        _playerView = [[KJPlayerView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) Configuration:configuration];
       _playerView.backgroundColor = [UIColor blackColor];
        _playerView.delegate=self;
        _playerView.videoIndex = 0;
    }
    return _playerView;
}
- (void)playSetModel {
    KJPlayerViewModel * model =[[KJPlayerViewModel alloc]init];
    model.name=@"考试视频";
    model.coverIamge=@"封面";
    model.priorityType=KJPlayerPlayTypeReplay;
    NSLog(@"视频地址 %@",self.videoPath);
    model.hd=[self get_url];
   self.playerView.videoModel=model;
}

- (NSURL *)get_url {
    NSURL *url = [NSURL URLWithString:self.videoPath];
    if (self.localVideo) {
        url = [NSURL fileURLWithPath:self.videoPath];
    }
    return url;
}
/// Top按钮事件 state:播放器状态  tag:200返回(左上角)、201功能按钮(右上角)
- (void)kj_PlayerView:(KJPlayerView*)playerView PlayerState:(KJPlayerState)state TopButton:(UIButton*)sender {
    if (sender.tag==200) {
        [self backClicked];
    }
}
- (void)backClicked {
     [self.playerView.player kj_playerStop];
     [self dismissViewControllerAnimated:YES completion:nil];
}

// 播放和暂停
- (void)playOrPauseAction:(UIButton*)sender{
    sender.selected = !sender.selected;
    if (self.playerView.configuration.state == KJPlayerStatePause || self.playerView.configuration.state == KJPlayerStatePlayEnd) {
        [self.playerView.player kj_playerResume];
    } else if(self.playerView.configuration.state == KJPlayerStatePlaying){
        [self.playerView.player kj_playerPause];
    }
}

//- (void)getPlayer {
//    NSString *path = self.videoPath;
//           //为即将播放的视频内容进行建模
//            AVPlayerItem *avplayerItem = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:path]];
//    //    //创建监听（这是一种KOV的监听模式）
////            [avplayerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
//    //        //给播放器赋值要播放的对象模型
//         AVPlayer *avplayer = [AVPlayer playerWithPlayerItem:avplayerItem];
//    //    //指定显示的Layer
//            AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:avplayer];
//    layer.frame=self.view.bounds;
////    self.playerView.player=layer;
//         [self.view.layer addSublayer:layer];
//
//        [avplayer play];
//}
@end
